zabbix监控nginx状态

        首先,在nginx的配置文件中添加如下一段代码

1
2
3
4
5
6
7
8
9
10
11
12
server
{
listen localhost:80;
server_name localhost;
location /nginxstatus {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.1.165;
deny all;
}
}

        保存之后重启nginx

        在zabbix脚本目录下创建脚本监控zabbix状态

1
cat nginx_status.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# Script to fetch nginx statuses for tribily monitoring systems
# Author: admin@huxianglin.cn
# License: GPLv2
# Set Variables
# Functions to return nginx stats
function active {
curl -s "http://localhost/nginx_status"| awk '/Active/{print $3}'
}
function reading {
curl -s "http://localhost/nginx_status"| awk '/Reading/{print $2}'
}
function writing {
curl -s "http://localhost/nginx_status"| awk '/Writing/{print $4}'
}
function waiting {
curl -s "http://localhost/nginx_status"| awk '/Waiting/{print $6}'
}
function accepts {
curl -s "http://localhost/nginx_status"| awk NR==3| awk '{print $1}'
}
function handled {
curl -s "http://localhost/nginx_status"| awk NR==3| awk '{print $2}'
}
function requests {
curl -s "http://localhost/nginx_status"| awk NR==3| awk '{print $3}'
}
# Run the requested function
case "$1" in
active)
active
;;
reading)
reading
;;
writing)
writing
;;
waiting)
waiting
;;
accepts)
accepts
;;
handled)
handled
;;
requests)
requests
;;
*)
echo "Usage: $0 {nginx_site_dicovery}"
echo "Usage: $0 {active [host] | reading [host] | writing [host] | waiting [host] | accepts [host] | handled [host] | requests [host]}"
esac

        然后修改该文件的属主和属组为zabbix,并且具有执行权限

1
2
chown zabbix.zabbix /usr/local/zabbix-2.4.4/scripts/nginx_status.sh
chmod 755 /usr/local/zabbix-2.4.4/scripts/nginx_status.sh

        然后在zabbix_agentd.conf配置文件中添加如下代码

1
2
3
4
5
6
7
8
#monitor nginx
UserParameter=nginx.accepts,/usr/local/etc/nginx_status.sh accepts
UserParameter=nginx.handled,/usr/local/etc/nginx_status.sh handled
UserParameter=nginx.requests,/usr/local/etc/nginx_status.sh requests
UserParameter=nginx.connections.active,/usr/local/etc/nginx_status.sh active
UserParameter=nginx.connections.reading,/usr/local/etc/nginx_status.sh reading
UserParameter=nginx.connections.writing,/usr/local/etc/nginx_status.sh writing
UserParameter=nginx.connections.waiting,/usr/local/etc/nginx_status.sh waiting

        这段代码可以视自己目录情况而改变

        创建好了之后就可以在web页面配置item监控项了